import time
from datetime import datetime

from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

# 创建浏览器驱动对象
driver= webdriver.Edge()
driver.maximize_window()
driver.implicitly_wait(5)
#打开页面
driver.get("http://localhost:82/index.php")

driver.find_element(By.CSS_SELECTOR,'.red').click()
driver.find_element(By.CSS_SELECTOR,'#username').send_keys("13800138006")
driver.find_element(By.CSS_SELECTOR,'#password').send_keys("123456")
driver.find_element(By.CSS_SELECTOR,'#verify_code').send_keys("8888")
driver.find_element(By.CSS_SELECTOR,"a[name='sbtbutton']").click()

time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"a[href='/index.php/Home/Index/index.html']").click()

# 搜索


driver.find_element(By.CSS_SELECTOR,'#q').send_keys("小米")

# 点击搜索
driver.find_element(By.CSS_SELECTOR,"button[type='submit']").click()
time.sleep(1)
# 点击第一个商品
driver.find_element(By.CSS_SELECTOR,"body > div:nth-child(7) > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > ul:nth-child(1) > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1)").click()

driver.find_element(By.CSS_SELECTOR,"#join_cart").click()
time.sleep(2)
# 切换frame
driver.switch_to.frame(1)
time.sleep(1)
span=WebDriverWait(driver,5).until(lambda driver:driver.find_element(By.CSS_SELECTOR,"div[class='conect-title'] span"))

# span=driver.find_element(By.CSS_SELECTOR,"div[class='conect-title'] span")
print(span.text)

driver.refresh()

gouwuche=driver.find_element(By.CSS_SELECTOR,"#hd-my-cart")
# ActionChains(driver).move_to_element(gouwuche).perform()
#
# goods_nums=driver.find_element(By.CSS_SELECTOR,"#total_qty")
# print(goods_nums.text)
#
# total_pay=driver.find_element(By.CSS_SELECTOR,"#total_pay")
# print(total_pay.text)

gouwuche.click()


goods_nums=driver.find_element(By.CSS_SELECTOR,"#goods_num")
print(goods_nums.text)
if goods_nums=='0':
    print("购物车空空的哦~，去看看心仪的商品吧~")

else:
    pass

# 加入购物车












time.sleep(10)


#关闭浏览器驱动对象
driver.quit()
